package com.exmaple.mapreducer.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
// k3 reduce输入的key，类型是Text
//v3 reduce输入的values 类型  IntWritable
//k4 reduce输出 类型 Text
//v4 reduce输出 类型IntWritable
//泛型：              k3      v3         k4       v4
public class WordCountReducer  extends Reducer<Text, IntWritable,Text,IntWritable> {

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //1.  k4=key
        Text k4 = key;
        //定义变量记录总数
        int total=0;
        //2. 遍历reduce输入的值即这个集合
        for (IntWritable value : values) {
             total+=value.get();
        }
       //定义reduce输出的键
        IntWritable v4 = new IntWritable();
        //给输出的键设置值，即在该例是单词的总和
        v4.set(total);
        //3. 输出
        context.write(k4, v4);


    }
}
